using Microsoft.EntityFrameworkCore;
using DeathReportManagement.Models;

namespace DeathReportManagement.Data;

public class DeathReportContext : DbContext
{
    public DeathReportContext(DbContextOptions<DeathReportContext> options) : base(options)
    {
    }
    
    public DbSet<User> Users { get; set; }
    public DbSet<DeathReport> DeathReports { get; set; }
    public DbSet<OperationLog> OperationLogs { get; set; }
    public DbSet<Dictionary> Dictionaries { get; set; }
    public DbSet<Icd10> Icd10s { get; set; }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        // User 配置
        modelBuilder.Entity<User>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.DoctorCode).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
            entity.Property(e => e.Department).HasMaxLength(100);
            entity.Property(e => e.Role).IsRequired().HasMaxLength(20).HasDefaultValue("Doctor");
            entity.Property(e => e.Password).HasMaxLength(255);
            entity.Property(e => e.IsActive).HasDefaultValue(true);
            entity.Property(e => e.CreateTime).HasDefaultValueSql("GETDATE()");
            entity.Property(e => e.UpdateTime).HasDefaultValueSql("GETDATE()");
            
            entity.HasIndex(e => e.DoctorCode).HasDatabaseName("IX_Users_DoctorCode");
        });
        
        // DeathReport 配置
        modelBuilder.Entity<DeathReport>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ReportNo).IsRequired().HasMaxLength(50);
            entity.Property(e => e.OutpatientNo).HasMaxLength(50);
            entity.Property(e => e.InpatientNo).HasMaxLength(50);
            entity.Property(e => e.DeceasedName).IsRequired().HasMaxLength(100);
            entity.Property(e => e.IsAnonymous).HasDefaultValue(false);
            entity.Property(e => e.Gender).IsRequired().HasMaxLength(10);
            entity.Property(e => e.Ethnicity).IsRequired().HasMaxLength(50);
            entity.Property(e => e.CountryOrRegion).HasMaxLength(50).HasDefaultValue("中国");
            entity.Property(e => e.IdType).IsRequired().HasMaxLength(50);
            entity.Property(e => e.IdNumber).IsRequired().HasMaxLength(50);
            entity.Property(e => e.AgeUnit).IsRequired().HasMaxLength(10);
            entity.Property(e => e.MaritalStatus).IsRequired().HasMaxLength(20);
            entity.Property(e => e.Education).IsRequired().HasMaxLength(50);
            entity.Property(e => e.PersonalIdentity).IsRequired().HasMaxLength(50);
            entity.Property(e => e.DeathLocation).IsRequired().HasMaxLength(100);
            entity.Property(e => e.IsPregnant).HasDefaultValue(false);
            entity.Property(e => e.WorkUnit).IsRequired().HasMaxLength(200);
            entity.Property(e => e.HouseholdAddressType).HasMaxLength(20).HasDefaultValue("本县区");
            entity.Property(e => e.HouseholdAddress).HasMaxLength(500);
            entity.Property(e => e.HouseholdAddressCode).HasMaxLength(50);
            entity.Property(e => e.HouseholdDetailAddress).IsRequired().HasMaxLength(500);
            entity.Property(e => e.ResidenceAddressType).IsRequired().HasMaxLength(20);
            entity.Property(e => e.ResidenceAddress).IsRequired().HasMaxLength(500);
            entity.Property(e => e.ResidenceDetailAddress).IsRequired().HasMaxLength(500);
            entity.Property(e => e.ContactName).IsRequired().HasMaxLength(100);
            entity.Property(e => e.ContactPhone).IsRequired().HasMaxLength(50);
            entity.Property(e => e.ContactIdType).HasMaxLength(50).HasDefaultValue("身份证");
            // 枚举字段使用默认配置
            entity.Property(e => e.CreateTime).HasDefaultValueSql("GETDATE()");
            
            entity.HasIndex(e => e.ReportNo).IsUnique().HasDatabaseName("IX_DeathReports_ReportNo");
            entity.HasIndex(e => e.CreateBy).HasDatabaseName("IX_DeathReports_CreateBy");
            entity.HasIndex(e => e.CreateTime).HasDatabaseName("IX_DeathReports_CreateTime");
            entity.HasIndex(e => e.AuditStatus).HasDatabaseName("IX_DeathReports_AuditStatus");
            entity.HasIndex(e => e.ReportStatus).HasDatabaseName("IX_DeathReports_ReportStatus");
            
            // 外键关系
            entity.HasOne(d => d.Creator)
                .WithMany(p => p.CreatedReports)
                .HasForeignKey(d => d.CreateBy)
                .OnDelete(DeleteBehavior.Restrict);
                
            entity.HasOne(d => d.Updater)
                .WithMany(p => p.UpdatedReports)
                .HasForeignKey(d => d.UpdateBy)
                .OnDelete(DeleteBehavior.Restrict);
                
            entity.HasOne(d => d.Auditor)
                .WithMany(p => p.AuditedReports)
                .HasForeignKey(d => d.AuditBy)
                .OnDelete(DeleteBehavior.Restrict);
        });
        
        // OperationLog 配置
        modelBuilder.Entity<OperationLog>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Operation).IsRequired().HasMaxLength(100);
            entity.Property(e => e.OperateTime).HasDefaultValueSql("GETDATE()");
            entity.Property(e => e.Remark).HasMaxLength(500);
            
            entity.HasIndex(e => e.ReportId).HasDatabaseName("IX_OperationLogs_ReportId");
            entity.HasIndex(e => e.UserId).HasDatabaseName("IX_OperationLogs_UserId");
            
            entity.HasOne(d => d.Report)
                .WithMany(p => p.OperationLogs)
                .HasForeignKey(d => d.ReportId)
                .OnDelete(DeleteBehavior.Cascade);
                
            entity.HasOne(d => d.User)
                .WithMany(p => p.OperationLogs)
                .HasForeignKey(d => d.UserId)
                .OnDelete(DeleteBehavior.Restrict);
        });
        
        // Dictionary 配置
        modelBuilder.Entity<Dictionary>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Type).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Code).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
            entity.Property(e => e.ParentCode).HasMaxLength(50);
            entity.Property(e => e.SortOrder).HasDefaultValue(0);
            entity.Property(e => e.IsActive).HasDefaultValue(true);
        });
    }
}